//
//  BVShoppingCartCell.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/26.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVShoppingCartCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Extension.h"

@interface BVShoppingCartCell ()

@end

@implementation BVShoppingCartCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupChildView];
    }
    return self;
}

- (void)setupChildView {
    
    // 单选按钮
    self.radioButton = [[UIButton alloc] init];
    [self.radioButton setImage:IMAGE_NAMED(@"huiduihao") forState:UIControlStateNormal];
    [self.radioButton setImage:IMAGE_NAMED(@"hongduihao") forState:UIControlStateSelected];
    [self.radioButton addTarget:self action:@selector(radioClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.radioButton];
    
    // 配图
    self.imgView = [[UIImageView alloc] init];
    self.imgView.layer.masksToBounds = YES;
    self.imgView.layer.cornerRadius = 5;
    self.imgView.layer.borderWidth = .5f;
    self.imgView.layer.borderColor = [UIColor BVTextLightGrayColor].CGColor;
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.imgView];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.contentView addSubview:self.titleLabel];
    
    // 促销
    self.salesLabel = [[UILabel alloc] init];
    self.salesLabel.layer.masksToBounds = YES;
    self.salesLabel.layer.borderWidth = 1;
    self.salesLabel.layer.borderColor = [UIColor cyanColor].CGColor;
    self.salesLabel.textColor = [UIColor cyanColor];
    self.salesLabel.textAlignment = NSTextAlignmentCenter;
    self.salesLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.salesLabel];
    
    // 删除
    self.deleteButton = [[UIButton alloc] init];
    [self.deleteButton setBackgroundImage:IMAGE_NAMED(@"shanchu") forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.deleteButton];
    
    // 单价
    self.unitPriceLabel = [[UILabel alloc] init];
    self.unitPriceLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.unitPriceLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.unitPriceLabel];
    
    // 重量(规格)
    self.weightLabel = [[UILabel alloc] init];
    self.weightLabel.font = [UIFont systemFontOfSize:14];
    self.weightLabel.textColor = [UIColor BVBaseOrangeColor];
    [self.contentView addSubview:self.weightLabel];
    
    // 商品价格
    self.allPriceLabel = [[UILabel alloc] init];
    self.allPriceLabel.font = [UIFont boldSystemFontOfSize:15];
    self.allPriceLabel.textColor = [UIColor BVBaseOrangeColor];
    [self.contentView addSubview:self.allPriceLabel];
    
    // 减
    self.minusButton = [[UIButton alloc] init];
    [self.minusButton setBackgroundImage:IMAGE_NAMED(@"jianhao_1") forState:UIControlStateNormal];
    [self.minusButton addTarget:self action:@selector(minusClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.minusButton];
    
    // 数量
    self.goodsNumLabel = [[UILabel alloc] init];
    self.goodsNumLabel.textColor = [UIColor BVBaseTextBlackColor];
//    [self.numLabel setBackgroundColor:[UIColor colorWithPatternImage:IMAGE_NAMED(@"huibian")]];
    self.goodsNumLabel.backgroundColor = [UIColor BVLineGrayColor];
    self.goodsNumLabel.textAlignment = NSTextAlignmentCenter;
    self.goodsNumLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.goodsNumLabel];
    
    // 加
    self.addButton = [[UIButton alloc] init];
    [self.addButton setBackgroundImage:IMAGE_NAMED(@"jiahao_1") forState:UIControlStateNormal];
    [self.addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.addButton];
    
    // 限购提示
    self.noticeLabel = [[UILabel alloc] init];
    self.noticeLabel.numberOfLines = 0;
    self.noticeLabel.font = [UIFont systemFontOfSize:14];
    self.noticeLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.noticeLabel.backgroundColor = COLOR_RGB(255, 207, 191);
    [self.contentView addSubview:self.noticeLabel];
    
    // 间距
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 5)];
    lineView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    [self.contentView addSubview:lineView];
}

#pragma mark -
#pragma mark - 刷新数据
- (void)setShoppingCart:(BVShoppingCart *)shoppingCart {
    _shoppingCart = shoppingCart;
    [self settingDataForChildView];
}

- (void)settingDataForChildView {
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:self.shoppingCart.img] placeholderImage:IMAGE_NAMED(@"placeholderImage.png")];
    self.titleLabel.text = self.shoppingCart.title;
    self.unitPriceLabel.text = [NSString stringWithFormat:@"%@元/%@",self.shoppingCart.unit_price,self.shoppingCart.unit];
    self.weightLabel.text = [NSString stringWithFormat:@"%@%@装",self.shoppingCart.weight,self.shoppingCart.unit];
    self.allPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.shoppingCart.all_price];
    self.noticeLabel.text = self.shoppingCart.notice;
    self.goodsNumLabel.text = [NSString stringWithFormat:@"%ld",self.shoppingCart.goods_num];
    
    if ([self.shoppingCart.is_sales isEqualToString:@"1"]) {
        [self.salesLabel setText:@"促销"];
    } else {
        [self.salesLabel setHidden:YES];
    }
    
    if (self.shoppingCart.goods_num == 1) {
        self.minusButton.enabled = NO;
    } else {
        self.minusButton.enabled = YES;
    }
    
    if ([self.shoppingCart.status isEqualToString:@"0"]) {
        [self.goodsNumLabel setHidden:YES];
        [self.addButton setHidden:YES];
        [self.minusButton setHidden:YES];
        [self.radioButton setImage:IMAGE_NAMED(@"shixiao") forState:UIControlStateNormal];
        [self.radioButton setEnabled:NO];
    } else {
        [self.goodsNumLabel setHidden:NO];
        [self.addButton setHidden:NO];
        [self.minusButton setHidden:NO];
        [self.radioButton setImage:IMAGE_NAMED(@"huiduihao") forState:UIControlStateNormal];
        [self.radioButton setEnabled:YES];
    }
}

#pragma mark -
#pragma mark - 计算frame
- (void)setShoppingCartFrame:(BVShoppingCartFrame *)shoppingCartFrame {
    _shoppingCartFrame = shoppingCartFrame;
    [self settingFrameForChildView];
}

- (void)settingFrameForChildView {
    self.radioButton.frame = self.shoppingCartFrame.radioButtonFrame;
    self.imgView.frame = self.shoppingCartFrame.imgViewFrame;
    self.titleLabel.frame = self.shoppingCartFrame.titleLabelFrame;
    self.salesLabel.frame = self.shoppingCartFrame.salesLabelFrame;
    self.unitPriceLabel.frame = self.shoppingCartFrame.unitPriceLabelFrame;
    self.weightLabel.frame = self.shoppingCartFrame.weightLabelFrame;
    self.allPriceLabel.frame = self.shoppingCartFrame.allPriceLabelFrame;
    self.noticeLabel.frame = self.shoppingCartFrame.noticeLabelFrame;
    self.deleteButton.frame = self.shoppingCartFrame.deleteButtonFrame;
    self.addButton.frame = self.shoppingCartFrame.addButtonFrame;
    self.goodsNumLabel.frame = self.shoppingCartFrame.goodsNumLabelFrame;
    self.minusButton.frame = self.shoppingCartFrame.minusButtonFrame;
}

#pragma mark - 
#pragma mark - 单选
- (void)radioClick:(UIButton *)button {
    button.selected = !button.selected;
    if (self.radioBlock) {
        self.radioBlock(self.indexPath,button.selected);
    }
}

#pragma mark -
#pragma mark - 加
- (void)addClick:(UIButton *)button {
    if (self.addBlock) {
        self.addBlock(self.indexPath);
    }
    NSInteger selectNum = self.shoppingCart.goods_num;
    NSInteger storeNum = self.shoppingCart.number;
    if (selectNum == storeNum - 1) return;
    self.shoppingCart.goods_num +=1;
    [self showNumber:self.shoppingCart.goods_num];
}

#pragma mark -
#pragma mark - 减
- (void)minusClick:(UIButton *)button {
    if (self.minusBlock) {
        self.minusBlock(self.indexPath);
    }
    self.shoppingCart.goods_num -=1;
    [self showNumber:self.shoppingCart.goods_num];
}

#pragma mark -
#pragma mark - 删除
- (void)deleteClick:(UIButton *)button {
}

- (void)showNumber:(NSInteger)count {
    [self.minusButton setEnabled:count == 1 ? NO : YES];
    self.goodsNumLabel.text = [NSString stringWithFormat:@"%lu",count];
}


@end
